package cc.iotkit.manager.controller;

import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.common.api.Request;
import cc.iotkit.common.satoken.utils.AuthUtil;
import cc.iotkit.common.utils.MapstructUtils;
import cc.iotkit.common.validate.QueryGroup;
import cc.iotkit.data.dao.AreaMappingRepository;
import cc.iotkit.data.dao.TbAreaRepository;
import cc.iotkit.data.model.TbArea;
import cc.iotkit.data.model.TbAreaMapping;
import cc.iotkit.data.model.TbDeviceGroup;
import cc.iotkit.manager.dto.bo.category.CategoryBo;
import cc.iotkit.model.area.Area;
import cc.iotkit.model.device.DeviceGroup;
import cc.iotkit.model.response.ResponseResult;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.date.DateTime;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
 * @Title: cc.iotkit.ledc.controller
 * @author: Sunne
 * @date: 2024-09-23 15:44
 * @Description: 区域控制器的主要前端视图
 */
@Api(tags = {"区域控制器"})
@Slf4j
@RestController
@RequestMapping("/area")
public class AreaController {
    @Autowired
    private TbAreaRepository tbAreaRepository;

    @ApiOperation("列表")
    @SaCheckPermission("iot:area:query")
    @PostMapping("/list")
    public Page<TbArea> getProducts(@Validated @RequestBody PageRequest<CategoryBo> request) {
        if(AuthUtil.isAdmin()){
            return tbAreaRepository.findAllByUid(Pageable.ofSize(request.getPageSize()).withPage(request.getPageNum()-1));
        }
        return tbAreaRepository.findAllByUid(AuthUtil.getUserId(), Pageable.ofSize(request.getPageSize()).withPage(request.getPageNum() - 1));
    }
    @ApiOperation("新建区域")
    @SaCheckPermission("iot:area:add")
    @PostMapping("/add")
    public ResponseResult<TbArea> addArea(@Validated @RequestBody Request<TbArea> tbArea) {
        tbArea.getData().setUid(AuthUtil.getUserId());
        tbArea.getData().setId(UUID.randomUUID().getMostSignificantBits());
        return ResponseResult.okHasData(tbAreaRepository.save(tbArea.getData()));
    }
    @ApiOperation("删除所选区域")
    @SaCheckPermission("iot:area:remove")
    @PostMapping("/remove")
    public ResponseResult<Void> delArea(@Validated Request<List<Long>> tbArea) {
        tbAreaRepository.deleteAllById(tbArea.getData());
        return ResponseResult.ok();
    }

    @ApiOperation("修改区域")
    @SaCheckPermission("iot:area:edit")
    @PostMapping("/edit")
    public ResponseResult<Void> edidArea(@Validated @RequestBody Request<TbArea> tbArea) {
        tbAreaRepository.save(tbArea.getData());
        return ResponseResult.ok();
    }
}